package com.example.school.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.school.dto.*;
import com.example.school.mapper.AdminMapper;
import com.example.school.pojo.Semester;
import com.example.school.service.SemesterService;
import com.example.school.mapper.SemesterMapper;
import com.example.school.utils.common.enums.Code;
import com.example.school.utils.common.result.PageResponseResult;
import com.example.school.utils.common.result.Result;
import com.example.school.utils.config.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 24354
* @description 针对表【semester】的数据库操作Service实现
* @createDate 2024-03-16 23:26:56
*/
@Service
@Slf4j
public class SemesterServiceImpl extends ServiceImpl<SemesterMapper, Semester>
    implements SemesterService{

    @Autowired
    private SemesterMapper semesterMapper;
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Result saveSemester(AdminSemesterSaveDto dto) {
        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        List<Semester> checkSemseterList = semesterMapper.selectList(Wrappers.<Semester>lambdaQuery()
                .eq(Semester::getSemesterMessage, dto.getSemesterMessage()));
        if (!CollectionUtils.isEmpty(checkSemseterList)&&checkSemseterList.size()>0) {
            return Result.errorResult(Code.DATA_EXIST);
        }
        Semester semester=new Semester();
        semester.setSemesterStart(dto.getSemesterStart());
        semester.setSemesterEnd(dto.getSemesterEnd());
        semester.setSemesterMessage(dto.getSemesterMessage());
        int checkSave = semesterMapper.insert(semester);
        if (checkSave==0) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result removeSemester(AdminSemesterRemoveDto dto) {

        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        Semester checkSemester = semesterMapper.selectById(dto.getSemesterId());
        if (checkSemester==null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        int checkRemove = semesterMapper.deleteById(dto.getSemesterId());
        if (checkRemove==0) {
            return Result.errorResult(Code.SERVER_ERROR);
        }

        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result updateGetSemester(AdminSemesterUpdateGetDto dto) {
        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        Semester checkSemester = semesterMapper.selectById(dto.getSemesterId());
        if (checkSemester==null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        return Result.okResult(checkSemester);
    }

    @Override
    public Result updateSemester(AdminSemesterUpdateDto dto) {
        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        Semester checkSemester = semesterMapper.selectById(dto.getSemesterId());
        Semester semester=new Semester();
        BeanUtils.copyProperties(checkSemester,semester);
        log.info("semester:{}",semester);
        log.info("dto:{}",dto);
        if (dto.getSemesterStart()!=null&&dto.getSemesterStart().length()!=0&&!dto.getSemesterStart().equals("")) {
            semester.setSemesterStart(dto.getSemesterStart());
        }
        if (dto.getSemesterEnd()!=null&&dto.getSemesterEnd().length()!=0&&!dto.getSemesterEnd().equals("")) {
            semester.setSemesterEnd(dto.getSemesterEnd());
        }
        if (dto.getSemesterMessage()!=null&&!dto.getSemesterMessage().equals("")) {
            semester.setSemesterMessage(dto.getSemesterMessage());
        }
        int checkUpdate = semesterMapper.updateById(semester);
        if (checkUpdate==0) {
            return Result.errorResult(Code.SERVER_ERROR);
        }

        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result getAdminSemester(AdminGetListDto dto) {
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        dto.checkParam();
        IPage page=new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<Semester> wrapper=new LambdaQueryWrapper<>();
        if (dto.getMimo()!=null) {
            wrapper.like(Semester::getSemesterMessage,dto.getMimo());
        }
        page=page(page,wrapper);
        Result result=new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());

        return result;
    }

}




